<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String mainid = request.getParameter("mainid");
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> 
<html xmlns="http://www.w3.org/1999/xhtml">
  <head> 							
		<%@ include file="../jslib/jquerylib.jsp" %>
		
		 <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jqGrid/css/jqgrid.css"></link>
	  <script type="text/javascript" src="<%=request.getContextPath() %>/jslib/lhgdialog/lhgcore.lhgdialog.min.js?skin=mac"></script>
	  <script type='text/javascript' src="<%=request.getContextPath() %>/jslib/My97DatePicker/WdatePicker.js" defer="defer"></script>
      <link href="<%=request.getContextPath() %>/web/style/layout.css" type="text/css" rel="stylesheet"/>
     <link rel="stylesheet" href="<%=request.getContextPath() %>/jslib/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css"/>
     <script type="text/javascript" src="<%=request.getContextPath() %>/jslib/ztree/jquery.ztree.core-3.4.js"></script>
    <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/demo.css">
	<script type="text/javascript" src="<%=request.getContextPath() %>/jslib/jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
		
	       <!-- 删除结果信息返回 -->
	    	<c:if test="${requestScope.message!=null}">
				<script type="text/javascript">
	  	 		     alert('<c:out value="${requestScope.message}"></c:out>');
	  	 		     window.close();
	  			</script>
	        </c:if>
    <script type="text/javascript">

	function selectperson(id){
		 var users = window.showModalDialog("<%=request.getContextPath() %>/resource/choose/chooseAllUser.jsp", window, "dialogWidth:900px;status:no;dialogHeight:500px");
   	if (users != null) {
   		var userIds = [];
           var userNames = [];
           for(var i=0;i<users.length;i++){
               userIds.push(users[i]['userid']);
               userNames.push(users[i]['username']);
           }
			    $("#"+id).val(userNames.join());
		        $("#"+id+"ID").val(userIds.join());
		   }
	} 
     var api = frameElement.api, W = api.opener;
     var ids = '${ids}';   
    $(document).ready(function(){
    	var lastrow=0;
    	var lastcell=0;
    	var $grid = $("#gridTableSample");
    	$("#gridTableSample").jqGrid({
        	url:'<%=request.getContextPath() %>/equipmentParts!queryDataList.action?ids='+ids,
			datatype: "json",
			height: 150,
			width: 720, 
			autowidth: true, 
			colNames:['序号','仓库名称（部门）','备件代码','备件名称','备件属性','备件等级','规格型号','品牌','供应商','平均价格',
						'平均采购周期','最大库存','最小库存','实际库存','库存状态提醒','计量单位','适用设备','适用设备编号','存放地点','备注','出库数量'],
			colModel:[
					{name:'id',index:'id', width:100, key:true,hidden:true,hidedlg:true}, 
					{name:'epStorehouse',index:'epStorehouse', width:100,hidden:true,hidedlg:true},
					{name:'epCode',index:'epCode', width:150},	
					{name:'epName',index:'epName', width:150},								
					{name:'epProperty',index:'epProperty', width:100,hidden:true},
					{name:'epLevel',index:'epLevel', width:100,hidden:true},
					{name:'epGuige',index:'epGuige', width:150},
					{name:'epBrand',index:'epBrand', width:100,hidden:true},
					{name:'epSupplier',index:'epSupplier', width:110,hidden:true,hidedlg:true},
					{name:'epAverageprice',index:'epAverageprice', width:60},
					{name:'epCycle',index:'epCycle', width:100,hidden:true},
					{name:'epMaxstock',index:'epMaxstock', width:60,editable:true,edittype:'text',hidden:true},
					{name:'epMinstock',index:'epMinstock', width:60,hidden:true},
					{name:'epActualstock',index:'epActualstock', width:60,classes:'biaohong',hidden:true},
					{name:'epStockstate',index:'epStockstate', width:100,hidden:true},
					{name:'epMeasurementunit',index:'epMeasurementunit', width:100,hidden:true},
					{name:'epApplicableequipment',index:'epApplicableequipment', width:100,hidden:true},
					{name:'epApplicableequipmentid',index:'epApplicableequipmentid', width:100,hidden:true},	
					{name:'epStoragelocation',index:'epStoragelocation', width:100,hidden:true}	,
					{name:'epRemark',index:'epRemark', width:100,hidden:true},
					{name:'epoutNum',index:'epoutNum',editable:true,edittype:'text',width:100},	
			],
			shrinkToFit:false,
			sortname:'epCode',
			sortorder:'desc',
			viewrecords:true,
			//multiselect: true, // 是否显示复选框
			multiboxonly : true, 
			rownumbers: true,//显示行号
			rownumWidth: 30, //行号的宽度
			rowNum:15,
			rowList:[15,20,50,400,510],
			toolbar: [false,"top"],
			beforeEditCell:function(rowid,cellname,v,iRow,iCol){
				lastrow = iRow;
				lastcell = iCol;
			},
			beforeSubmitCell:function(rowid,cellname,value,iRow,iCol){
				var r = /^\+?[1-9][0-9]*$/;			
				if(!r.test(value)){
					alert("请输入数据不合法！");
					this.restoreCell();
				}			
				//通过rowid 查询库存不大于库存数量即可。
				var isValid = true;
				var param ={"rid":rowid,"inputNum":value};
				$.ajax({
		            type: "POST",
		            async:false,  
		            url: '<%=request.getContextPath()%>/equipmentpartsout!validateData.action',
		            data:param,
		            dataType: "json", 
				    error : function(textStatus, errorThrown) {  
			          alert("系统ajax交互错误: " + textStatus.value);  
			      },  
			      success : function(data, textStatus) {
			      	if(data.result=='success'){
			      		//alert("数字正确！");       
			      	}
			      	else{
				      	isValid = false;			      					      	
			      	}
			    } 
		        });
				//判断value为数字
				//var stockNum='${stockNum}';
			   // 	 if(parseInt(value)>parseInt(stockNum)){
			   // 		alert("请输入数据不合法！");
				//		this.restoreCell();
			    //	    }	
					if(!isValid){
						alert("出库数量大于实际库存，请核实出库数量!");
						this.restoreCell();
					}
			    			    	  
				},     
			jsonReader: {
				root:"rows",		// 数据行（默认为：rows）
				page: "page",  	// 当前页
				total: "total",  // 总页数
				records: "records",  // 总记录数
				repeatitems : false		// 设置成false，在后台设置值的时候，可以乱序。且并非每个值都得设
			},
			prmNames:{rows:"rows",page:"page",sort:"sidx",order:"sord",search:"search"},
			pager:"#gridPagerSample",
			caption: "设备备件台账",
			cellEdit:true,
			cellsubmit:'clientArray', 
		});
		//下面是提交表单代码
    	var actionUrl = '<%=request.getContextPath()%>/equipmentpartsout!saveData.action';
		var options = {
	  	    url:actionUrl,
	  	     beforeSubmit: function() {
	   				return checking();
	    			},
	  	    dataType:'json',
	  	    success: function(data) {	  	    
	  	      if(data.result=='success'){
				alert("提交成功");		
				api.close();
				W.reload();
	  	      }
	  	    }};
    	$('#saveBtn').click(function(){
    		//jQuery("#gridTableSample").jqGrid('restoreRow',"1");
    		$("#gridTableSample").jqGrid("saveCell",lastrow,lastcell);
    		getGridDataNoAlert();
    		$('#pageFrom').ajaxSubmit(options); //提交表单
	            return false;
		});
		$('#closeBtn').click(function(){
			api.close();
		});

	
		//选择人员

	  	function checking(){
	   		var flag = true;
			var FormSerial = $("#outFormSerial").val();
			var flowname = $("#flowname").val();
			var applyPerson = $("#applyPerson").val();
			var applyDept = $("#applyDept").val();
			var person = $("#user").val();
			var housekeeper = $("#user1").val();				
			if(FormSerial ==null || FormSerial == "" ){
				alert("出库单号不能为空！");
				$("#outFormSerial").focus();
	    		return false;
			}
			if(flowname ==null || flowname == "" ){
				alert("任务名称不能为空！");
				$("#OutType").focus();
	    		return false;
			}		
			if(applyPerson ==null || applyPerson == "" ){
				alert("申请人不能为空！");
				$("#OutType").focus();
	    		return false;
			}
			if(applyDept ==null || applyDept == ""){
				alert("申请部门不能为空！");
				$("#applyDept").focus();
	    		return false;
			}
			if(person ==null || person == ""){
				alert("审批科长不能为空！");
				$("#user").focus();
	    		return false;
			}

			if(housekeeper ==null || housekeeper == ""){
				alert("仓管员不能为空！");
				$("#user1").focus();
	    		return false;
			}		
			//var validGridData = getGridData();	//对grid中出库数据进行验证。
			//if(validGridData==false){
			//	return false;
			//}	
			return getGridData();						
		}  

	  	function getGridData(){
		  	var flag = true;
		  	var gridData = "";
	  	    var obj = $("#gridTableSample").jqGrid("getRowData");// 获取jqGrid当前所有数据	  	   
	  	    jQuery(obj).each(function(){
	  	    	//gridData = gridData+';'+this.id+','+ this.epoutNum;
				//判断出库数量是否为空
				if(this.epoutNum ==null || this.epoutNum == ""){
					alert("出库数量不能为空！");
					flag = false;
					return  false;//跳出循环				
				}
	  	    });
	  	 	return flag; // 整个函数返回值	   
	  	}
		function getGridDataNoAlert(){
		  	var flag = true;
		  	var gridData = "";
	  	    var obj = $("#gridTableSample").jqGrid("getRowData");// 获取jqGrid当前所有数据	  	   
	  	    jQuery(obj).each(function(){
	  	    	gridData = gridData+';'+this.id+','+ this.epoutNum + ',' + this.epCode+',' + this.epName +',' + this.epGuige + ',' + this.epAverageprice;	  	    	
	  	    });
	  	 	$('#gridData').val(gridData); //给隐藏input赋值 
	  	 	return flag; // 整个函数返回值   
	  	}	  	                 	
});  		
   </script>
 	<style type="text/css">
 		body{
 			background:#ffffff; background-image:url(images/);
 		}
   		html{
		    overflow-x: hidden;
			overflow-y: hidden;
		}  	
		.formTableNew textarea{border: 1px solid #999;padding: 5px;width: 760px;-moz-border-radius: 3px;border-radius: 3px;margin-bottom: 5px;color: #666;background: url(../images/input_bg.gif) repeat-x top;}
   </style>
  </head>
  
 <body>
<form id="pageFrom" action="" enctype="multipart/form-data" method="post">
        <input type="hidden" id="flag" name="flag" />
        <input type="hidden" name="flowid" id="flowid" value="${flowid}"/>
     	<input type="hidden" name="stateid" id="stateid" value="${stateid}"/>
     	<input type="hidden" name="outstoreform.applyid"" id ="applyid" value ="${applyid}"/>
     	${steptcontent}
  		<table width="100%" border="0" align="center" cellpadding="5" cellspacing="5" class="formTableNew">
  			
  		 <tr>			   
  			    <td align="right">任务名称：</td>
  			    <td align="left">
  			    <input  type="text"  id="flowname" maxlength="50" name="flowname" style="border: 1px solid #999;padding: 5px;width: 250px;" value ="备件出库流程"/>
  			  	<img src="<%=request.getContextPath() %>/images/xh.png" align="absmiddle"></img>
  			    </td>
  			
  			  <td align="right">出库单号：</td>
  			  <td align="left">
  			    <input  type="text" id="outFormSerial" maxlength="50" name="outstoreform.formserial" style="border: 1px solid #999;padding: 5px;width: 250px" value ="${outSerial}" readonly="readonly"/>
  			    <img src="<%=request.getContextPath() %>/images/xh.png" align="absmiddle"></img>
  			 	<input  type="hidden" id="gridData" name="gridData"  value="${gridData}"/>
  			  </td> 			 
  		 </tr>
  		<tr>   
  			  <td align="right">申请部门：</td>
  			  <td align="left">
  			    <input  type="text" id="applyDept" maxlength="50" name="outstoreform.applydept" style="border: 1px solid #999;padding: 5px;width: 250px"  value="${applyDep}"/>
  			    <img src="<%=request.getContextPath() %>/images/xh.png" align="absmiddle"></img>
  			  </td>
  			  
  			  
  			   <td align="right">出库类型：</td>
  			   <td align="left">
  			    <select id="OutType" name="outstoreform.outtype"/>
  			        <option>维修</option>
  			        <option>维保</option>
  			        <option>改造</option>
  			        <option>借用</option>
  			        <option>报废</option>
  			        <option>其他</option>
  			    </select>
  			    <img src="<%=request.getContextPath() %>/images/xh.png" align="absmiddle"></img>
  			   </td>
  		</tr>
  			 <tr>  
  			   <td align="right">申请日期：</td>
  			  <td align="left">
  			  	 <input  type="text" id="gongyingpinname" maxlength="50" name="outstoreform.applydate"  onClick="WdatePicker()" value = "${timeStr}" style="border: 1px solid #999;padding: 5px;width: 250px"/>
  			  	 <img src="<%=request.getContextPath() %>/images/xh.png" align="absmiddle"></img>
  			 </td>
  			  
             <td align="right">申请人：</td>
  			   <td align="left">
  			    <input type="text" id="applyPerson" maxlength="50" name="outstoreform.applyperson" style="border: 1px solid #999;padding: 5px;width: 250px"  value="${applyPerson}"/>
  			    <img src="<%=request.getContextPath() %>/images/xh.png" align="absmiddle"></img>
  			  </td> 			   
  			</tr>
  			
  			<tr>
  			  			   
  			  <td align="right">出库原因：</td>
  			  <td align="left">
  			    <input  type="text"  id="chukuyuanyin" maxlength="50"  name="outstoreform.reasonforout" style="border: 1px solid #999;padding: 5px;width: 250px;"/>
  			  </td> 
  			  
  			    			   
  			   <td align="right">备注：</td>
  			  <td align="left">
  			    <input  type="text"  id="beizhu" maxlength="50"  name="outstoreform.remarks" style="border: 1px solid #999;padding: 5px;width: 250px;"/>
  			  </td> 
  			  
  			</tr> 												
  			 <!-- 备件列表 -->
  			<tr>
			<td colspan='4' align='center'>
			<div id='tt1' class='easyui-tabs' style='width:750px;height：600px'>
			<div title='备         件 ' style='padding:10px'>
				<table id="gridTableSample"></table>
				<div id=gridPagerSample></div></div>
			</td>
		   </tr> 		   
		   	<tr>
  			  <td align="right">选择科长：</td>
  			  	<td align="left">
  			  	 <input type="hidden" id="userID" name="outstoreform.leaderid" />
  			  	 <input  type="text"  id="user" name="outstoreform.leadername" maxlength="50" style="border: 1px solid #999;padding: 5px;width: 200px;height:20px;" />
  			     <img src="<%=request.getContextPath() %>/images/xh.png"  align="absmiddle"></img>
  			    <img src="<%=request.getContextPath() %>/web/images/form_icon_1.gif" style="cursor:hand; vertical-align: middle;" onclick="selectperson('user');" />
  			  </td>
  			  
  			  <td align="right">选择仓管员：</td>
  			  	 <td align="left">
  			  	 <input type="hidden" id="user1ID" name="outstoreform.housekeeperid" />
  			  	 <input  type="text"  id="user1" name="outstoreform.housekeepername" maxlength="50" style="border: 1px solid #999;padding: 5px;width: 200px;height:20px;" />
  			     <img src="<%=request.getContextPath() %>/images/xh.png"  align="absmiddle"></img>
  			     <img src="<%=request.getContextPath() %>/web/images/form_icon_1.gif" style="cursor:hand; vertical-align: middle;" onclick="selectperson('user1');" />
  			   </td>  
  			</tr> 		
		</table>
		<table width="100%" border="0" align="center" class="formTableNew">
		
			<td align="center">
		    	<input id="saveBtn" type="button" value="提 交" class="form_bt_orange"/>
		    	<input type="button" id="closeBtn" value="取 消" class="form_bt_grey" />
		    </td>
		  </tr>		  
		</table>
	<!--表单结束-->
	</form>
  </body>
</html>
